package gui.tree
{
	import app.FurnitureType;
	import com.bit101.components.VBox;
	import events.TreeEvents;
	import fl.controls.Button;
	import fl.controls.Label;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import gui.buttons.RadioButton;
	import gui.factory.StyleFactory;
	import utils.ErrorsCollector;
	
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	
	 /* When facade texture changed
	 * @eventType flash.events.Event
	 */
	[Event(name = "ITEM_SELECTED", type = "events.TreeEvents")]
	public class TreeFoldersView extends Sprite
	{
		public var title:String;
		
		private var _titleText:Label;
		
		private var _foldersRB:Vector.<RadioButton>;
		private var _nodesRB:Vector.<RadioButton>;
		private var nodeFolderInfo:Vector.<NodeFolder>;
		private var _btnVertSpacing:Number;
		private var _bntStartY:Number;
		private var _currentExpanded:int = -1;
		private var _currentSelected:int = -1;
		private const RB_NODE_GROUP_NAME:String = "RADIO_BUTTON_GROUP_NAME";
		private const RB_FOLDER_GROUP_NAME:String = "RB_FOLDER_GROUP_NAME";
		
		public function TreeFoldersView(treeTite:String)
		{
			title = treeTite;
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
		}
		
		private function handleAddedToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
			initTree();
		}
		
		private function initTree():void
		{
			_titleText = new Label();
			addChild(_titleText);
			_titleText.text = title;
			
			var btnTopPadding:Number = 7;
			_btnVertSpacing = 1;
			_bntStartY = _titleText.y + _titleText.height + btnTopPadding;
			
			nodeFolderInfo = new Vector.<NodeFolder>();
			_foldersRB = new Vector.<RadioButton>();
			_nodesRB = new Vector.<RadioButton>();
		}
		
		public function addFolder(folderNode:NodeFolder):void
		{
			nodeFolderInfo.push(folderNode);
			_foldersRB.push(createFolderNode(folderNode.title,folderNode));
		}
		
		private function createFolderNode(title:String, tag:NodeFolder):RadioButton
		{
			var btn:RadioButton = new RadioButton();
			btn.label = title;
			btn.value = tag;
			StyleFactory.setRadioButtonStyle(btn, StyleFactory.STYLE_RB_TREE_FOLDER);
			btn.addEventListener(Event.CHANGE, handleFolderChange);
			btn.groupName = RB_FOLDER_GROUP_NAME;
			return btn;
		}
		
		private function handleFolderChange(e:Event):void 
		{
			var index:int = _foldersRB.indexOf(e.currentTarget);
			dispatchEvent(new TreeEvents(TreeEvents.FOLDER_SELECTED));
			if (index > -1)
			{
				expand(index);
			}
		}
		
		private function expand(index:int):void 
		{
			closeRadioButtons();
			normalView();
			_currentExpanded = index;
			
			var radBtnLeftPadding:Number = 5;
			var radBtnTopPadding:Number = 5;
			var radBtnVertSpacing:Number = 5;
			var radBtnTextWidth:Number = 150;
			var localStartY:Number = 0;
			for (var i:int = 0; i < _foldersRB.length; i++)
			{
				_foldersRB[i].validateNow();
				_foldersRB[i].x = 0;
				if (i == 0)
				{
					_foldersRB[i].y = _bntStartY;
				}else if ((i - index) == 1)
				{
					_foldersRB[i].y =localStartY;
				}else
				{
					_foldersRB[i].y = _foldersRB[i - 1].getBoundRectComponent().height 
									+ _foldersRB[i-1].y + _btnVertSpacing;
				}
				
				if (i == index)
				{
					// show radio buttons
					var radBntStartY:Number = _foldersRB[i].y + _foldersRB[i].getBoundRectComponent().height 
											+ radBtnTopPadding;
					var lastY:Number = 0;
					for (var j:int = 0; j < nodeFolderInfo[i].items.length; j++ )
					{
						var radBtn:RadioButton = new RadioButton();
						StyleFactory.setRadioButtonStyle(radBtn, StyleFactory.STYLE_RB_TREE_NODE);
						radBtn.groupName = RB_NODE_GROUP_NAME;
						radBtn.value = nodeFolderInfo[i].items[j];
						radBtn.label =  nodeFolderInfo[i].items[j].title;
						radBtn.validateNow();
						_nodesRB.push(radBtn);
						radBtn.addEventListener(Event.CHANGE, handleRadioButtonChange);
						if (j == 0)
						{
							radBtn.y = radBntStartY;
							radBtn.selected = true;
						}else
						{
							radBtn.y = _nodesRB[j - 1].getBoundRectComponent().height + 
										_nodesRB[j - 1].y +
										radBtnVertSpacing;
						}
						
						
						lastY = radBtn.getBoundRectComponent().height + radBtn.y 
										+ radBtnVertSpacing;
						addChild(radBtn);
					}
					
					localStartY = lastY	+ _btnVertSpacing;
					
				}
			}
		}
		
		private function handleRadioButtonChange(e:Event):void 
		{
			
			var radBtn:RadioButton = e.currentTarget as RadioButton;
			if (radBtn != null)
			{
				var nodeRadBtn:NodeRB = radBtn.value as NodeRB;
				if (nodeRadBtn != null)
				{
					dispatchEvent(new TreeEvents(TreeEvents.ITEM_SELECTED));
				}else
				{
					ErrorsCollector.collectError("Treeview selected rad btn NodeRadioButton: null");
				}
			}
		}
		
		
		private function closeRadioButtons():void
		{
			for each(var radioBtn:RadioButton in _nodesRB)
			{
				if (radioBtn.parent != null)
				{
					radioBtn.parent.removeChild(radioBtn);
				}
			}
			_nodesRB = new Vector.<RadioButton>();
			_currentSelected = -1;
		}
		
		private function normalView():void
		{
			var radBtnVertSpacing:Number = 5;
			for (var i:int = 0; i < _foldersRB.length; i++)
			{
				_foldersRB[i].validateNow();
				if (i == 0)
				{
					_foldersRB[i].y = _bntStartY;
				}else
				{
					_foldersRB[i].y = _foldersRB[i-1].getBoundRectComponent().height + 
										_foldersRB[i-1].y +
										_btnVertSpacing;
					//_foldersRB[i].y = _foldersRB[i-1].height + _foldersRB[i-1].y + _btnVertSpacing;
				}
			}
		}
		
		public function updateView():void
		{
			
			for (var i:int = 0; i < _foldersRB.length; i++)
			{
				if (_foldersRB[i].parent != null)
				{
					_foldersRB[i].parent.removeChild(_foldersRB[i]);
				}
				addChild(_foldersRB[i]);
				_foldersRB[i].x = 0;
				if (i == 0)
				{
					_foldersRB[i].y = _bntStartY;
				}else
				{
					_foldersRB[i].y = _foldersRB[i-1].height + _foldersRB[i-1].y + _btnVertSpacing;
				}
			}
			
			for each(var radioBtn:RadioButton in _nodesRB)
			{
				if (radioBtn.parent != null)
				{
					radioBtn.parent.removeChild(radioBtn);
				}
			}
		}
		
		public function getSelectedRB():NodeRB
		{
			if (_nodesRB.length > 0)
			{
				for each(var radBtn:RadioButton in _nodesRB)
				{
					if (radBtn.selected)
					{
						return radBtn.value as NodeRB;
					}
				}
				return null;
			}else
			{
				return null;
			}
		}
		
		public function getSelectedFolder():NodeFolder
		{
			if (_foldersRB.length > 0)
			{
				for each(var radBtn:RadioButton in _foldersRB)
				{
					if (radBtn.selected)
					{
						return radBtn.value as NodeFolder;
					}
				}
				return null;
			}else
			{
				return null;
			}
		}
		
		public function get foldersRB():Vector.<RadioButton> 
		{
			return _foldersRB;
		}
		
		public function set foldersRB(value:Vector.<RadioButton>):void 
		{
			_foldersRB = value;
		}
	
	}

}